package com.zhangchenhui.jmh;

import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import java.util.Collections;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.concurrent.TimeUnit;

/**
 * @author 张晨辉
 * @Classname JMHExample01
 * @Description TODO
 * @Date 2020/8/11 下午11:00
 */
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@BenchmarkMode(Mode.AverageTime)
@Threads(5)
@Fork(1)
@Measurement(iterations = 5)
@Warmup(iterations = 5)
@State(Scope.Benchmark)
public class JMHExample08 {

    private Map<Long,Long> map ;

    //为 type 提供了四种可配置的参数值
    @Param ( {"1","2","3","4"})
    private int type ;

    @Setup(Level.Trial)
    public void setUp(){
        switch (type){
            case 1 :
                this.map= new ConcurrentHashMap<>();
                break ;
            case 2:
                this.map = new ConcurrentSkipListMap<>() ;
                break ;
            case 3 :
                this.map= new Hashtable<>();
                break ;
            case 4 :
                this.map= Collections.synchronizedMap(new HashMap<> ());
                break ;
            default :
                throw new IllegalArgumentException("");
        }
    }
    @Benchmark
    public void test() throws InterruptedException {
        this.map.put(System.nanoTime(),System.nanoTime());
    }

    public static void main(String[] args) throws RunnerException {
        final Options opts = new OptionsBuilder()
                .include(JMHExample08.class.getSimpleName())
                .build();
        new Runner(opts).run();


    }
}
